<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla view library
jimport('joomla.application.component.view');
 
/**
 * NGMenu View
 */
class NGMenuViewNGMenu extends JView
{
	var $ngitems;
	
	/**
	 * display method of NGMenu view
	 * @return void
	 */
	public function display($tpl = null) 
	{	
		// get the Data
		$form = $this->get('Form');
		$item = $this->get('Item');
		
		//set the user state for the correct font weight selection in the field weightvalues
		$app = JFactory::getApplication();
		$app->setUserState( "com_ngmenu.font_family", $item->font_family );

		// Check for errors.
		if (count($errors = $this->get('Errors'))) 
		{
			JError::raiseError(500, implode('<br />', $errors));
			return false;
		}
		// Assign the Data
		$this->form = $form;
		$this->item = $item;

		// Set the toolbar
		$this->addToolBar();

		// init some background color fields
		if (empty($this->item->id)) {
			$this->initMenuFields();
		}

		$this->syncNGData();
		
		// Display the template
		parent::display($tpl);
	}
 
	/**
	 * Sync data of the menu
	 */
	protected function syncNGData()
	{
		
		$db = JFactory::getDBO();

		$query = $db->getQuery(true);
		$query->select('*');
		$query->from('#__ngmenu_item');
		$query->where("ngmenu_id = '".$this->item->id."'");
		$db->setQuery($query);
		$menu_items = $db->loadObjectList();
		
		//sync menu items
		if($menu_items){
			foreach($menu_items as $index => $menu_item){
				$query = $db->getQuery(true);
				$query->select('id');
				$query->from('#__menu');
				$query->where(array("id='".$menu_item->item_id."'", "published='1'") );
				$db->setQuery($query);
				$item_exists = $db->loadResult();
				
				if(is_null($item_exists)){
					$query = $db->getQuery(true);
					$query->delete('#__ngmenu_item');
					$query->where(array("ngmenu_id='".$this->item->id."'", "item_id='".$menu_item->item_id."'") );
					$db->setQuery($query);
					$db->query();
				}
			}
		}
		
		JLoader::import( 'items', JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_menus' . DS . 'models' );
		$menumodel = JModel::getInstance( 'Items', 'MenusModel', array('ignore_request' => true));
		
		$menutype = $this->item->name;
		if(empty($menutype)){
			$query = $db->getQuery(true);
			$query->select('menutype');
			$query->from('#__menu_types');
			$query->order('menutype');
			$db->setQuery($query);
			$menutype = $db->loadResult();
		}
		$menumodel->setState('filter.menutype', $menutype);
		$menumodel->setState('filter.published', '1');
		$menu_items = $menumodel->getItems();
		
		$this->item->id = empty($this->item->id) ? '0' : $this->item->id;
		$parent = null;
		$default_icons = array();
		$default_istyles = array();
		$items = array();
		JLoader::import('joomla.utilities.arrayhelper');
		$ngitem = JModelAdmin::getInstance( 'NGItem', 'NGMenuModel' );
		
		foreach($menu_items as $index => $menu_item){
			$is_separator = ($menu_item->type == 'separator') ? 1 : 0;
			if($is_separator){
				if(substr_count($menu_item->title, '-') < strlen($menu_item->title)/2){
					$is_separator = 0;
				}
			}

			$items[$menu_item->id] = JModelAdmin::getInstance( 'NGItem', 'NGMenuModel' );
			$items[$menu_item->id]->id = 0;
			$items[$menu_item->id]->ngmenu_id = $this->item->id;
			$items[$menu_item->id]->item_id = $menu_item->id;
			$items[$menu_item->id]->parent_id = $menu_item->parent_id;
			$items[$menu_item->id]->label = $menu_item->title;
			$items[$menu_item->id]->is_separator = $is_separator;
			$items[$menu_item->id]->itemstyle_id = 0;
			$items[$menu_item->id]->smstyle_id = 0;
			$items[$menu_item->id]->tooltip = '';
			$items[$menu_item->id]->icon_normal = '';
			$items[$menu_item->id]->icon_over = '';
			$items[$menu_item->id]->icon_active = '';
			$items[$menu_item->id]->default_icon = 0;
			$items[$menu_item->id]->default_istyle = 0;
			$items[$menu_item->id]->assign_substyle = false;
			if($parent != $menu_item->parent_id && !$is_separator){
				$items[$menu_item->id]->assign_substyle = true;
				$parent = $menu_item->parent_id;
			}

			$data = JArrayHelper::fromObject($items[$menu_item->id]);
			$ngitem->save($data);
			$items[$menu_item->id] = JArrayHelper::toObject($data, 'JObject');
		}
		// assign the id value of newly created records
		$ngitems = $ngitem->getItem('ngmenu_id', $this->item->id, $getlist=true);
		foreach ($ngitems as $ngitem){
			$items[$ngitem->item_id]->id = $ngitem->id;
		}
		
		$this->ngitems = $items;
		//end sync

	}
	
	/**
	 * Initialize default values for the menu preview
	 */
	protected function initMenuFields()
	{
		// init background color fields on new menu records (problem with transparent 
		// setting when the default value was set by the xml file)
		$this->form->setValue('menu_back_color', null, '');
		$this->form->setValue('item_back_color_normal', null, '');
		$this->form->setValue('item_back_color_over', null, '#99CCCC');
//		$this->form->setValue('item_back_color_active', null, '#FFCC99');
		$this->form->setValue('main_arrow_image_normal', null, '/modules/mod_ngmenu/mod_ngmenu/images/arrow/arr_black2_right.gif');
		$this->form->setValue('main_arrow_image_over', null, '/modules/mod_ngmenu/mod_ngmenu/images/arrow/arr_white2_right.gif');
//		$this->form->setValue('main_arrow_image_active', null, '/modules/mod_ngmenu/mod_ngmenu/images/arrow/arr_white2_right.gif');
		$this->form->setValue('sub_arrow_image_normal', null, '/modules/mod_ngmenu/mod_ngmenu/images/arrow/arr_black2_right.gif');
		$this->form->setValue('sub_arrow_image_over', null, '/modules/mod_ngmenu/mod_ngmenu/images/arrow/arr_white2_right.gif');
//		$this->form->setValue('sub_arrow_image_active', null, '/modules/mod_ngmenu/mod_ngmenu/images/arrow/arr_white2_right.gif');
		$this->form->setValue('menu_back_image', null, '/modules/mod_ngmenu/mod_ngmenu/images/background/bg_99cccc.gif');
		$this->form->setValue('item_back_image_normal', null, '/modules/mod_ngmenu/mod_ngmenu/images/background/back_blue1.gif');
		$this->form->setValue('item_back_image_over', null, '/modules/mod_ngmenu/mod_ngmenu/images/background/back_blue1.gif');
//		$this->form->setValue('item_back_image_active', null, '/modules/mod_ngmenu/mod_ngmenu/images/background/back_blue1.gif');
		$this->form->setValue('separator_vimage', null, '/modules/mod_ngmenu/mod_ngmenu/images/separator/sep_black.gif');
		
		// init fields for the menu preview on a new menu record
		$this->item->name = 'mainmenu';
		$this->item->template = JText::_('COM_NGMENU_LIST_VALUES_INDEPENDENT');
		$this->item->path_prefix_img = '';
		$this->item->path_prefix_link = '';
		$this->item->is_horizontal = 0;
		$this->item->menu_align = 0;
		$this->item->sm_align = 'left';
		$this->item->sm_valign = 'top';
		$this->item->save_navigation_path = 1;
		$this->item->show_by_click = 0;
		$this->item->no_wrap = 1;
		$this->item->dm_write_all = 1;
		$this->item->dynamic = 1;
		$this->item->sm_hide_pause = 800;
		$this->item->sm_show_pause = 200;
		$this->item->sm_columns = 1;
		$this->item->sm_orientation = 0;
		$this->item->pressed_item = -1;
		$this->item->item_cursor = 'pointer';
		$this->item->item_target = '_self';
		$this->item->status_string = 'link';
		$this->item->blank_image = '/modules/mod_ngmenu/mod_ngmenu/images/blank.gif';
		$this->item->menu_width = '100';
		$this->item->menu_width_unit = '%';
		$this->item->menu_height = '';
		$this->item->submenu_width = '';
		$this->item->submenu_height = '';
		$this->item->main_separator_create = 0;
		$this->item->sub_separator_create = 0;
		$this->item->separator_himage = '';
		$this->item->separator_hwidth = '100';
		$this->item->separator_hwidth_unit = '%';
		$this->item->separator_hheight = 3;
		$this->item->separator_halign = 'left';
		$this->item->separator_vimage = '';
		$this->item->separator_vwidth = 5;
		$this->item->separator_vheight = '100';
		$this->item->separator_vheight_unit = '%';
		$this->item->arrow_width = 7;
		$this->item->arrow_height = 7;
		$this->item->main_arrow_image_create = 1;
		$this->item->main_arrow_image_normal = '/modules/mod_ngmenu/mod_ngmenu/images/arrow/arr_black2_right.gif';
		$this->item->main_arrow_image_over = '/modules/mod_ngmenu/mod_ngmenu/images/arrow/arr_white2_right.gif';
//		$this->item->main_arrow_image_active = '/modules/mod_ngmenu/mod_ngmenu/images/arrow/arr_white2_right.gif';
		$this->item->sub_arrow_image_create = 1;
		$this->item->sub_arrow_image_normal = '/modules/mod_ngmenu/mod_ngmenu/images/arrow/arr_black2_right.gif';
		$this->item->sub_arrow_image_over = '/modules/mod_ngmenu/mod_ngmenu/images/arrow/arr_white2_right.gif';
//		$this->item->sub_arrow_image_active = '/modules/mod_ngmenu/mod_ngmenu/images/arrow/arr_white2_right.gif';
		$this->item->absolute_position = 0;
		$this->item->menu_back_color = '';
		$this->item->menu_back_image_create = '1';
		$this->item->menu_back_image = '/modules/mod_ngmenu/mod_ngmenu/images/background/bg_99cccc.gif';
		$this->item->menu_back_image_repeat = 'repeat-y';
		$this->item->menu_border_style = 'solid';
		$this->item->menu_border_color = '#99CCCC';
		$this->item->menu_border_width = 1;
		$this->item->item_back_color_normal = '';
		$this->item->item_back_color_over = '#99CCCC';
//		$this->item->item_back_color_active = '#FFCC99';
		$this->item->item_back_image_create = 0;
		$this->item->item_back_image_normal = '';
		$this->item->item_back_image_over = '';
//		$this->item->item_back_image_active = '';
		$this->item->item_border_style_normal = 'none';
		$this->item->item_border_style_over = 'none';
//		$this->item->item_border_style_active = 'solid';
		$this->item->item_border_width = 1;
		$this->item->item_border_color_normal = '#FCEEB0';
		$this->item->item_border_color_over = '#339999';
//		$this->item->item_border_color_active = '#CC9900';
		$this->item->item_align_main = 'left';
		$this->item->item_align_sub = 'left';
		$this->item->item_spacing = 2;
		$this->item->item_padding_top = 5;
		$this->item->item_padding_right = 15;
		$this->item->item_padding_bottom = 5;
		$this->item->item_padding_left = 28;
		$this->item->font_weight = 'normal';
		$this->item->font_size = 12;
		$this->item->font_size_unit = 'px';
		$this->item->font_family = 'verdana,arial,helvetica,sans-serif';
		$this->item->font_color_normal = '#000000';
		$this->item->font_color_over = '#FFFFFF';
//		$this->item->font_color_active = '#996600';
		$this->item->font_decoration_normal = 'none';
		$this->item->font_decoration_over = 'none';
//		$this->item->font_decoration_active = 'none';
		$this->item->font_color_disabled = '#AAAAAA';
		$this->item->effect_transparency_create = 0;
		$this->item->effect_transparency = 80;
		$this->item->effect_transition_create = 1;
		$this->item->effect_trans_duration = 400;
		$this->item->effect_trans_duration2 = 400;
		$this->item->sm_collapse = 0;
		$this->item->update_config = 0;
		$this->item->css_create = 0;
		$this->item->show_active_state = 1;
	}
	
	/**
	 * Setting the toolbar
	 */
	protected function addToolBar() 
	{
		JRequest::setVar('hidemainmenu', true);
		$toolbar = & JToolBar::getInstance('toolbar');
		$toolbar->appendButton( 'Link', 'preview', JText::_('COM_NGMENU_PREVIEW'), "javascript:open_preview()" );
		JToolBarHelper::divider();
		$isNew = ($this->item->id == 0);
		JToolBarHelper::title($isNew ? JText::_('COM_NGMENU_MENU_CONFIG')." ".JText::_('COM_NGMENU_NEW') : JText::_('COM_NGMENU_MENU_CONFIG')." ".JText::_('COM_NGMENU_EDIT'), 'editngm');
		JToolBarHelper::apply('ngmenu.apply', 'JTOOLBAR_APPLY');
		JToolBarHelper::save('ngmenu.save', 'JTOOLBAR_SAVE');
		JToolBarHelper::cancel('ngmenu.cancel', $isNew ? 'JTOOLBAR_CANCEL' : 'JTOOLBAR_CLOSE');
	}
}
